package com.woniuxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.pojo.Menu;
import com.woniuxy.pojo.MenuRole;
import com.woniuxy.pojo.RespBean;
import com.woniuxy.pojo.Role;
import com.woniuxy.service.IMenuRoleService;
import com.woniuxy.service.IMenuService;
import com.woniuxy.service.IRoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName PermissionController 权限设计
 * @Description T0D0
 * @Author ykq11
 * @Date 2022/2/27 19:36
 * @Version 1.0
 **/
@RestController
@RequestMapping("/system/basic/permiss")
public class PermissionController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IMenuService  menuService;
    @Autowired
    private IMenuRoleService menuRoleService;
    @GetMapping("/")
    @ApiOperation(value = "获取所有的角色")
    public List<Role> getRoles() {
        return roleService.list();
    }
    @ApiOperation(value = "添加角色")
    @PostMapping("/")
    public RespBean addRole(@RequestBody Role role) {
        if(!role.getName().startsWith("ROLE_")){
            role.setName("ROLE_"+role.getName());
        }
        if(roleService.save(role)){
            return RespBean.success("添加成功");
        }
        return RespBean.error("添加失败");
    }
    @ApiOperation(value = "删除角色")
    @DeleteMapping("/role/{rid}")
    public RespBean deleteRole(@PathVariable Integer rid){
        if(roleService.removeById(rid)){
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }
    @ApiOperation(value = "查询所有菜单")
    @GetMapping("/menus")
    public  List<Menu> getAllMenus(){
         return menuService.getAllMenus();
    }
    @ApiOperation(value = "根据角色ID查询菜单id")
    @GetMapping("/mid/{rid}")
    public List<Integer> getMidByAdminId(@PathVariable Integer rid) {
        return menuRoleService.list(new QueryWrapper<MenuRole>().
                eq("rid",rid)).stream().map(MenuRole::getMid).collect(Collectors.toList());
    }
    @ApiOperation(value = "更新角色菜单")
    @PutMapping("/")
    public RespBean updateMenuRole(Integer rid,Integer[] mids){
        return menuService.updateMenuRole(rid, mids);
    }
}
